home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u118.dms / in.adf / disass / disass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-05  |  20.0 KB  |  769 lines

  1. /* (c) 1990 Martin Combs */
  2. /* Edited by Steve Hawtin to use as part of the dump program 
  3.  
  4.   This file defines the function
  5.  
  6.     void
  7.     disass(length,fptr)
  8.         int length;
  9.         FILE *fptr;
  10.  
  11. this function will read the next "length" elements of the file using the 
  12. function my_fread() and will create a disassembly listing using the 
  13. function my_printf() to print the data out.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18.  
  19. typedef void (*voidfun)();
  20.  
  21. #ifdef NEED_MAIN
  22. #define maxfile  20000
  23.  
  24. #endif
  25.  
  26. long i, iold;
  27. long *lp;
  28. unsigned int hibyte, lobyte, lonib, L76, mode, reg, reg1, size;
  29. short *wp;
  30. char *p;
  31. char instS[40], opS[20], op1S[40], op2S[20], ascS[20];
  32. char objS[40];
  33. char reg1S[4];
  34. char *ccS[16] =
  35.    {  "t",  "f", "hi", "ls",
  36.      "cc", "cs", "ne", "eq",
  37.      "vc", "vs", "pl", "mi",
  38.      "ge", "lt", "gt", "le" 
  39.     };
  40. char *shiftS[8]= 
  41.    { "asr",  "asl", "lsr", "lsl",
  42.     "roxr", "roxl", "ror", "rol"
  43.     };
  44.  
  45. binst()
  46. { switch (L76)
  47.     { case 0: strcpy(instS,"btst"); break;
  48.       case 1: strcpy(instS,"bchg"); break;
  49.       case 2: strcpy(instS,"bclr"); break;
  50.       case 3: strcpy(instS,"bset");
  51.     }
  52. }
  53.  
  54. sizef()
  55. { switch (L76)
  56.      { case 0: strcat(instS,".b"); size=0; break;
  57.        case 1: strcat(instS,".w"); size=1; break;
  58.        case 2: strcat(instS,".l"); size=2;
  59.      }
  60. }
  61.  
  62. sizeff()
  63. { sizef(); eff();
  64. }
  65.  
  66. eff()
  67.    {mode = (lobyte&56) >> 3;
  68.     reg = lobyte & 7;
  69.     eff1();
  70.     }
  71.  
  72. eff1()
  73.    {switch (mode)
  74.        {case 0: 
  75.             sprintf(opS,"d%d",reg);
  76.             break;
  77.         case 1:
  78.             sprintf(opS,"a%d",reg);
  79.             break;
  80.         case 2:
  81.             sprintf(opS,"(a%d)",reg);
  82.             break;
  83.         case 3:
  84.             sprintf(opS,"(a%d)+",reg);
  85.             break;
  86.         case 4:
  87.             sprintf(opS,"-(a%d)",reg);
  88.             break;
  89.         case 5:
  90.             i+=2;
  91.             wp=(short *)&p[i];
  92.             sprintf(opS,"%d(a%d)",*wp,reg);
  93.             break;
  94.         case 6:
  95.             i+=2;
  96.             sprintf(opS,"%d(a%d,",p[i+1],reg);
  97.             ariwi2();
  98.             break;
  99.         case 7: 
  100.             grabbag();
  101.         }
  102.     }
  103.  
  104. ariwi2()
  105.    {char regS[6];
  106.  
  107.     sprintf(regS,"%c%d.%c)",(p[i]&128)?'a':'d',((p[i] & 0x70) >> 4),
  108.             (p[i]&8)?'l':'w');
  109.     strcat(opS,regS);
  110.     }
  111.  
  112. grabbag()
  113. { switch (reg)
  114.     { case 0: i+=2; wp=(short *)&p[i]; sprintf(opS,"%d",*wp); break;
  115.       case 1: i+=4; lp=(long *)&p[i-2]; sprintf(opS,"%ld",*lp); break;
  116.       case 2: i+=2; wp=(short *)&p[i]; sprintf(opS,"%d(pc)",*wp); break;
  117.       case 3: i+=2; sprintf(opS,"%d(pc),",p[i+1]); ariwi2(); break;
  118.       default: switch (size)
  119.                 { case 0: i+=2; sprintf(opS,"#$%x",p[i+1]); break;
  120.                   case 1: i+=2; wp=(short *)&p[i];
  121.                           sprintf(opS,"#$%x",*wp); break;
  122.                   case 2: i+=4; lp=(long *)&p[i-2];
  123.                           sprintf(opS,"#$%lx",*lp); break;
  124.                 }
  125.     }
  126. }
  127.  
  128. movep()
  129. { if (lobyte & 64) strcpy(instS,"movep.l"); else strcpy(instS,"movep.w");
  130.   dreg();  reg=lobyte & 7; i+=2;
  131.   wp=(short *)&p[i];
  132.   if (lobyte & 128) {strcpy(op1S,reg1S);sprintf(op2S,"%d(a%d)",*wp,reg);}
  133.   else { strcpy(op2S,reg1S); sprintf(op1S,"%d(a%d)",*wp,reg); }
  134. }
  135.  
  136. dreg()
  137. { reg1S[0]='d';
  138.   reg1=(hibyte & 14) >> 1;
  139.   reg1S[1]=reg1+48;
  140. }
  141. areg()
  142. { reg1S[0]='a';
  143.   reg1=(hibyte & 14) >> 1;
  144.   reg1S[1]=reg1+48;
  145. }
  146.  
  147. move()
  148. { unsigned int inst;
  149.   inst=hibyte*256+lobyte;
  150.   if (size) { strcpy(instS,"move");
  151.               if ((inst & 448)==64) strcat(instS,"a");
  152.               if (size==2) strcat(instS,".l");
  153.               else strcat(instS,".w");
  154.               }
  155.   eff(); strcpy(op1S,opS); mode=(inst & 448)>>6; reg=(inst & 3584) >> 9;
  156.   eff1(); strcpy(op2S,opS);
  157. }
  158.  
  159. movec()
  160. { int contreg;
  161.   if (p[i]&128) opS[0]='a'; else opS[0]='d';
  162.   opS[1]=48 + ((p[i] & 112) >> 4);
  163.   opS[2]='\0';
  164.   wp=(short *)&p[i]; contreg=(*wp)&4095;
  165.   switch (contreg)
  166.    { case 0: strcpy(op1S,"sfc"); break;
  167.      case 1: strcpy(op1S,"dfc"); break;
  168.      case 2048: strcpy(op1S,"usp"); break;
  169.      case 2049: strcpy(op1S,"vbr"); break;
  170.    }
  171. }
  172.  
  173. movem()
  174.    {char rdS[20], registerlistS[30];
  175.     register j;
  176.     int bm, bm1=0;
  177.     int state = 0;
  178.   
  179.     if (lobyte & 64) 
  180.         strcpy(instS,"movem.l");
  181.       else
  182.         strcpy(instS,"movem.w");
  183.     eff();
  184.     i+=2;
  185.     wp=(short *)&p[i];
  186.     bm=*wp;
  187.  
  188.     registerlistS[0] = '\0';
  189.  
  190.     /* Scan the bits to see which registers we have */
  191.     for(j=0;j<16;++j)
  192.        {/* Look at this bit */
  193.         if(state==0 && (bm & 1<<(15-j)))
  194.            {/* We have a register */
  195.             sprintf(rdS,registerlistS[0]=='\0'?"%c%d":"/%c%d",
  196.                         j>=8?'a':'d',j%8);
  197.             strcat(registerlistS,rdS);
  198.             state = 1;
  199.             }
  200.           else if (state == 1)
  201.            {if((bm & 1<<(15-j))==0)
  202.                 state = 0;
  203.               else if (j==7 || j==15 || (bm & 1<<(14-j))==0)
  204.                {/* So this is the last of a set */
  205.                 sprintf(rdS,"-%c%d",j>=8?'a':'d',j%8);
  206.                 strcat(registerlistS,rdS);
  207.                 state = 0;
  208.                 }
  209.             }
  210.         }
  211.     if (lonib==8)
  212.        {strcpy(op1S,registerlistS);
  213.         strcpy(op2S,opS);
  214.         }
  215.       else
  216.        {strcpy(op1S,opS);
  217.         strcpy(op2S,registerlistS);
  218.         }
  219.     }
  220.  
  221. dbcc()
  222. { if (lonib==1) strcpy(instS,"dbra");
  223.   else { strcpy(opS,"db"); strcat(opS,instS); strcpy(instS,opS); }
  224.   op1S[0]='d'; op1S[1]=48 + (lobyte & 7); op1S[2]='\0';
  225.   i+=2; figurelabel();  sprintf(op2S,"%d",*wp);
  226.   strcat(op2S,opS);
  227. }
  228.  
  229. figurelabel()
  230. { wp=(short *)&p[i];
  231.  sprintf(opS,"      ($%lx)",iold + 2 + *wp);
  232. }
  233.  
  234. genasc()
  235.    {int asc, ii, k=0;
  236.     for (ii=iold; ii<i; ii++)
  237.        {asc=p[ii];
  238.         if (isprint(asc) && !(asc & 0x80))
  239.             ascS[k++]=asc;
  240.           else
  241.             ascS[k++]='.';
  242.         }
  243.     ascS[k]='\0';
  244.     }
  245.  
  246. genobj()
  247.    {/* Print out the object code as hex numbers */
  248.     int  ii;
  249.     char pr[4];
  250.  
  251.     objS[0] = '\0';
  252.     for (ii=0; (ii+iold)<i; ii++)
  253.        {
  254.         sprintf(pr,"%02x",p[ii+iold] & 0xff);
  255.         strcat(objS,pr);
  256.         if(ii%2)
  257.             strcat(objS," ");
  258.         }
  259.     }
  260.  
  261. void
  262. opinvalid()
  263.    {
  264.     strcpy(instS,"INVALID");
  265.     }
  266.  
  267. void
  268. op0000xxx0()
  269. { if (hibyte==8)
  270.       { binst(); i+=2; sprintf(op1S,"#$%x",p[i+1]&31);
  271.         eff(); strcpy(op2S,opS); return; }
  272.   if (hibyte==14) { strcpy(instS,"moves"); i+=2;
  273.                     if (p[i]&128) opS[0]='a'; else opS[0]='d';
  274.                     opS[1] = 48 + ((p[i] & 112) >> 4);
  275.                     opS[2] = '\0';
  276.                     wp=(short *)&p[i];
  277.                     if ((*wp)&2048)
  278.                        { strcpy(op1S,opS); sizeff(); strcpy(op2S,opS); }
  279.                     else {strcpy(op2S,opS); sizeff(); strcpy(op1S,opS); }
  280.                     return;
  281.                   }
  282.   switch (hibyte)
  283.     { case 0: strcpy(instS,"ori"); break;
  284.       case 2: strcpy(instS,"andi"); break;
  285.       case 4: strcpy(instS,"subi"); break;
  286.       case 6: strcpy(instS,"addi"); break;
  287.       case 10: strcpy(instS,"eori"); break;
  288.       case 12: strcpy(instS,"cmpi");
  289.     }
  290.   switch (L76)
  291.     { case 0: sprintf(op1S,"#$%x",p[i+3]&255); break;
  292.       case 1: wp=(short *)&p[i+2]; sprintf(op1S,"#$%x",*wp); break;
  293.       case 2: i+=2; lp=(long *)&p[i]; sprintf(op1S,"#$%lx",*lp);
  294.     }
  295.     i+=2; sizeff();
  296.     if (mode==7 && reg==4) { i-=2; if (!L76) { strcpy(op2S,"ccr"); return; }
  297.     else { strcpy(op2S,"sr"); return; }
  298.     }  /* end if (mode==7 etc */
  299.     strcpy(op2S,opS);
  300. }
  301.  
  302. void
  303. op0000xxx1()
  304. { if ((lobyte & 56)==8) { movep();return;}
  305.   binst();
  306.   dreg();
  307.   strcpy(op1S,reg1S);
  308.   eff();
  309.   strcpy(op2S,opS);
  310. }
  311.  
  312. void
  313. op0000()
  314. {
  315.   if (hibyte&1) op0000xxx1(); else op0000xxx0();
  316. }
  317.  
  318. void
  319. op0001()
  320. { strcpy(instS,"move.b"); size=0; move();
  321. }
  322.  
  323. void
  324. op0010()
  325. { size=2; move();
  326. }
  327.  
  328. void
  329. op0011()
  330. { size=1; move();
  331. }
  332.  
  333. void
  334. op01001000()
  335. { if (!L76) {strcpy(instS,"nbcd"); eff(); strcpy(op1S,opS); return; }
  336.   if ((L76==1) && (!(lobyte & 56))) strcpy(instS,"swap");
  337.   else strcpy(instS,"pea");
  338.   if (L76==1) { eff(); strcpy(op1S,opS); return; }
  339.   if (!(lobyte&56)) { strcpy(instS,"ext");
  340.                       if (L76==2) strcat(instS,".w");
  341.                       else if (L76==3) strcat(instS,".l");
  342.                       op1S[0]='d'; op1S[1]=48+(lobyte&7);
  343.                       op1S[2]='\0'; return; }
  344.   movem();
  345. }
  346.  
  347. void
  348. op01001010()
  349. { if (lobyte==252) { strcpy(instS,"illegal"); return; }
  350.   if (L76==3) { strcpy(instS,"tas"); eff(); strcpy(op1S,opS); return; }
  351.   strcpy(instS,"tst"); sizeff(); strcpy(op1S,opS);
  352. }
  353.  
  354. void
  355. op0100xxx0()
  356.    {
  357.     switch (lonib)
  358.        {case 0:
  359.             if (L76==3)
  360.                {strcpy(instS,"move");
  361.                 strcpy(op1S,"sr");
  362.                 eff();
  363.                 strcpy(op2S,opS);
  364.                 }
  365.               else 
  366.                {strcpy(instS,"negx");
  367.                 sizeff();
  368.                 strcpy(op1S,opS);
  369.                 }
  370.             return;
  371.         case 2:
  372.             if (L76==3)
  373.                {strcpy(instS,"move");
  374.                 strcpy(op1S,"ccr");
  375.                 eff();
  376.                 strcpy(op2S,opS);
  377.                 }
  378.               else
  379.                {strcpy(instS,"clr");
  380.                 sizeff();
  381.                 strcpy(op1S,opS);
  382.                 }
  383.             return;
  384.         case 4:
  385.             if (L76==3)
  386.                {strcpy(instS,"move");
  387.                 eff();
  388.                 strcpy(op1S,opS);
  389.                 strcpy(op2S,"ccr");
  390.                 }
  391.               else
  392.                {strcpy(instS,"neg");
  393.                 sizeff();
  394.                 strcpy(op1S,opS);
  395.                 }
  396.             return;
  397.         case 6:
  398.             if (L76==3)
  399.                {strcpy(instS,"move");
  400.                 eff();
  401.                 strcpy(op1S,opS);
  402.                 strcpy(op2S,"sr");
  403.                 }
  404.               else
  405.                {strcpy(instS,"not");
  406.                 sizeff();
  407.                 strcpy(op1S,opS);
  408.                 }
  409.             return;
  410.         case 8:
  411.             op01001000();
  412.             return;
  413.         case 10: 
  414.             op01001010();
  415.             return;
  416.         case 12:
  417.             movem();
  418.             return;
  419.         }
  420.     switch (lobyte & 0xF0)
  421.        {case 64:
  422.             strcpy(instS,"trap");
  423.             sprintf(op1S,"#$%x",lobyte&15);
  424.             return;
  425.         case 80:
  426.             sprintf(op1S,"a%d",lobyte & 7);
  427.             if (lobyte & 8)
  428.                 strcpy(instS,"unlk");
  429.               else 
  430.                {strcpy(instS,"link");
  431.                 i+=2;
  432.                 wp=(short *)&p[i];
  433.                 sprintf(op2S,"#$%x",*wp);
  434.                 }
  435.             return;
  436.         case 96:
  437.             strcpy(instS,"move");
  438.             sprintf(opS,"a%d",lobyte & 7);
  439.             if (lobyte & 8)
  440.                {strcpy(op1S,"usp");
  441.                 strcpy(op2S,opS);
  442.                 }
  443.               else
  444.                {strcpy(op1S,opS);
  445.                 strcpy(op2S,"usp");
  446.                 }
  447.             return;
  448.         }
  449.     switch (lobyte)
  450.        {case 112:
  451.             strcpy(instS,"reset");
  452.             return;
  453.       case 113: strcpy(instS,"nop");
  454.             return;
  455.       case 114: strcpy(instS,"stop"); wp=(short *)&p[i+2];
  456.                 sprintf(op1S,"#$%x",*wp); i+=2;
  457.             return;
  458.       case 115: strcpy(instS,"rte"); break;
  459.             return;
  460.       case 116: strcpy(instS,"rtd"); i+=2; wp=(short *)&p[i];
  461.                 sprintf(op1S,"#$%x",*wp);
  462.             return;
  463.       case 117: strcpy(instS,"rts");
  464.             return;
  465.       case 118: strcpy(instS,"trapv");
  466.             return;
  467.       case 119: strcpy(instS,"rtr");
  468.             return;
  469.       case 122: strcpy(instS,"movec"); i+=2; movec();
  470.                 strcpy(op2S,opS);
  471.             return;
  472.       case 123: strcpy(instS,"movec"); i+=2; movec(); strcpy(op2S,op1S);
  473.                 strcpy(op1S,opS);
  474.             return;
  475.     }
  476.     if (L76==2)
  477.        {strcpy(instS,"jsr");
  478.         eff();
  479.         strcpy(op1S,opS);
  480.         }
  481.       else if (L76==3)
  482.        {strcpy(instS,"jmp");
  483.         eff();
  484.         strcpy(op1S,opS);
  485.         }
  486.       else
  487.         opinvalid();
  488.     }
  489.  
  490. void
  491. op0100xxx1()
  492.    {if (L76==2)
  493.        {strcpy(instS,"chk");
  494.         dreg();
  495.         }
  496.       else if (L76==3)
  497.        {strcpy(instS,"lea");
  498.         areg();
  499.         }
  500.       else
  501.         opinvalid();
  502.     eff();
  503.     strcpy(op1S,opS);
  504.     strcpy(op2S,reg1S);
  505.     }
  506.  
  507. void
  508. op0100()
  509. { if (hibyte & 1) op0100xxx1(); else op0100xxx0();
  510. }
  511.  
  512. void
  513. op0101()
  514. { int quick;
  515.   if (L76==3) { strcpy(instS,ccS[lonib]);
  516.      if ((lobyte&56)==8) { dbcc(); return; }
  517.      else { strcpy(opS,"s"); strcat(opS,instS); strcpy(instS,opS);
  518.             eff(); strcpy(op1S,opS); return; }
  519.   }
  520.   if (lonib & 1) strcpy(instS,"subq"); else strcpy(instS,"addq");
  521.   sizeff(); strcpy(op2S,opS);
  522.   quick=(lonib & 14) >> 1;
  523.   if (!quick) quick=8;
  524.   op1S[0]='#'; op1S[1]=48+quick; op1S[2]='\0';
  525. }
  526.  
  527. void
  528. op0110()
  529. { if (!lonib) strcpy(instS,"bra");
  530.   else if (lonib==1) strcpy(instS,"bsr");
  531.   else { strcpy(instS,"b"); strcat(instS,ccS[lonib]);}
  532.   if (!lobyte) { i+=2; strcat(instS,".w"); figurelabel();
  533.                  sprintf(op1S,"%d%s",*wp,opS); return; }
  534.   strcat(instS,".s");
  535.   sprintf(op1S,"%-5d ($%lx)",p[i+1],iold + 2 + p[i+1]);
  536. }
  537.  
  538. void
  539. op0111()
  540.    {strcpy(instS,"moveq"); 
  541.     sprintf(op1S,"#%d",p[i+1]);
  542.     dreg(); 
  543.     strcpy(op2S,reg1S);
  544.     }
  545.  
  546. void
  547. op1000()
  548. { if (L76==3) { if (hibyte & 1) strcpy(instS,"divs");
  549.                 else strcpy(instS,"divu");
  550.                 size=1; dreg(); eff(); strcpy(op1S,opS);
  551.                 strcpy(op2S, reg1S); return; }
  552.   if (hibyte & 1) { if (!(lobyte & 48)) { strcpy(instS,"sbcd"); dreg();
  553.                        if (lobyte & 8) { sprintf(op1S,"-(a%d)",lobyte & 7);
  554.                                          sprintf(op2S,"-(a%d)",reg1); }
  555.                        else { sprintf(op1S,"d%d",lobyte & 7);
  556.                               strcpy(op2S,reg1S); }
  557.                                          }
  558.                     else { strcpy(instS,"or"); dreg(); sizeff();
  559.                            strcpy(op1S,reg1S); strcpy(op2S,opS); }
  560.                    }
  561.    else { strcpy(instS,"or"); dreg(); sizeff();
  562.           strcpy(op1S,opS); strcpy(op2S,reg1S); }
  563. }
  564.  
  565. void
  566. op1001()
  567. { if (L76==3) { if (lonib & 1) { strcpy(instS,"suba.l"); size=2; }
  568.                 else {strcpy(instS,"suba.w"); size=1; }
  569.       areg(); eff(); strcpy(op1S,opS); strcpy(op2S,reg1S); return;  }
  570.   if (!(lonib & 1)) { strcpy(instS,"sub"); dreg(); sizeff();
  571.                       strcpy(op1S,opS); strcpy(op2S,reg1S); }
  572.  
  573.   if (lonib & 1) { if (!(lobyte & 48)) { strcpy(instS,"subx");
  574.                                          sizef(); dreg();
  575.                        if (lobyte & 8) { sprintf(op1S,"-(a%d)",lobyte & 7);
  576.                                 sprintf(op2S,"-(a%d)",reg1); }
  577.                      else { sprintf(op1S,"d%d",lobyte & 7);
  578.                             strcpy(op2S,reg1S); }
  579.                     }
  580.                    else { strcpy(instS,"sub"); sizeff(); dreg();
  581.                          strcpy(op1S,reg1S); strcpy(op2S,opS); }
  582.   }
  583. }
  584.  
  585. void
  586. op1011()
  587. { if (L76==3) { strcpy(instS,"cmpa");
  588.       if (lonib & 1) { size=2; strcat(instS,".l"); }
  589.       else { size=1; strcat(instS,".w"); }
  590.       areg(); eff(); strcpy(op1S,opS); strcpy(op2S,reg1S); return; }
  591.   if (lonib & 1) { mode=(lobyte & 56) >> 3;
  592.                    if (mode==1) { strcpy(instS,"cmpm"); sizeff(); dreg();
  593.                      sprintf(op1S,"(%s)+",opS);
  594.                      sprintf(op2S,"(a%d)+",reg1); }
  595.                    else { strcpy(instS,"eor"); sizeff(); dreg();
  596.                           strcpy(op1S,reg1S); strcpy(op2S,opS); }
  597.                   }
  598.   else { strcpy(instS,"cmp"); sizeff(); dreg();
  599.          strcpy(op1S,opS); strcpy(op2S,reg1S); }
  600. }
  601.  
  602. void
  603. op1100()
  604. { if (L76==3)  {
  605.     if (lonib & 1) strcpy(instS,"muls"); else strcpy(instS,"mulu");
  606.     size=1; dreg(); eff(); strcpy(op1S,opS); strcpy(op2S,reg1S); return; }
  607.   if (!(lonib & 1)) { strcpy(instS,"and"); dreg(); sizeff();
  608.                       strcpy(op1S,opS); strcpy(op2S,reg1S); return; }
  609.   if (!(lobyte & 48)) {
  610.     if (!L76) { strcpy(instS,"abcd"); dreg(); eff();
  611.       if (mode==1) { sprintf(op1S,"-(%s)",opS);
  612.                      sprintf(op2S,"-(a%d)",reg1); return; }
  613.       else { strcpy(op1S,opS); strcpy(op2S,reg1S); return; }
  614.     }
  615.     strcpy(instS,"exg"); eff(); dreg();
  616.     if (L76==2) { strcpy(op1S,reg1S); strcpy(op2S,opS); return; }
  617.     if (mode==1) { strcpy(op1S,opS); sprintf(op2S,"a%d",reg1); }
  618.     else { strcpy(op1S,opS); strcpy(op2S,reg1S); }
  619.     return;
  620.   }
  621.   strcpy(instS,"and"); dreg(); sizeff();
  622.   strcpy(op1S,reg1S); strcpy(op2S,opS);
  623. }
  624.  
  625. void
  626. op1101()
  627. { if (L76==3) { strcpy(instS,"adda");
  628.      if (lonib & 1) { size=2; strcat(instS,".l"); }
  629.      else { size=1; strcat(instS,".w");  }
  630.      areg(); eff(); strcpy(op1S,opS); strcpy(op2S,reg1S); return;
  631.      }
  632.   if (!(lonib & 1)) { strcpy(instS,"add"); dreg(); sizeff();
  633.                     strcpy(op1S,opS); strcpy(op2S,reg1S); return; }
  634.   if (lobyte & 48) {strcpy(instS,"add"); sizeff(); dreg();
  635.                     strcpy(op1S,reg1S); strcpy(op2S,opS); return; }
  636.   strcpy(instS,"addx"); sizeff(); dreg();
  637.   if (mode==1) { sprintf(op1S,"-(%s)",opS); sprintf(op2S,"-(a%d)",reg1); }
  638.   else { strcpy(op1S,opS); strcpy(op2S,reg1S); }
  639. }
  640.  
  641. void
  642. op1110()
  643. { int sh;
  644.   if (L76==3) { sprintf(instS,"%s.w",shiftS[lonib]); eff();
  645.                 strcpy(op1S,opS); return; }
  646.   dreg(); sprintf(op2S,"d%d",lobyte & 7);
  647.   sh= ((lobyte & 24) >> 2) + (lonib & 1);
  648.   strcpy(instS,shiftS[sh]);
  649.   sizef();
  650.   if (lobyte & 32) { strcpy(op1S,reg1S); return; }
  651.   if (!reg1) reg1=8;
  652.   sprintf(op1S,"#$%x",reg1);
  653. }
  654.  
  655. voidfun opxxxx[16] =
  656.    {op0000,op0001,op0010,op0011,
  657.     op0100,op0101,op0110,op0111,
  658.     op1000,op1001,opinvalid,op1011,
  659.     op1100,op1101,op1110,opinvalid
  660.     };
  661.  
  662. void
  663. disass(length,fptr)
  664.     int  length;
  665.     FILE *fptr;
  666.    {
  667.     unsigned int hinib;
  668.  
  669.     i = 0;
  670.     p = calloc(length,sizeof(long));
  671.     if(p==NULL)
  672.        {my_printf("malloc failed\n");
  673.         exit(20);
  674.         }
  675.     my_fread(p,length*sizeof(long),fptr);
  676.  
  677.     while (i<length*sizeof(long))
  678.        { /* main loop */
  679.         iold=i;
  680.         hibyte=p[i]&255;
  681.         lobyte=p[i+1]&255;
  682.         L76=lobyte>>6&15;
  683.         hinib=hibyte>>4;
  684.         lonib=hibyte&15;
  685.         /* initialize strings */
  686.         instS[0]='\0';
  687.         op1S[0]='\0';
  688.         op2S[0]='\0';
  689.         /* Call the function selected by the hi nibble */
  690.         (*opxxxx[hinib])();
  691.         /* If there were two ops then concatenate them */
  692.         if (strlen(op1S))
  693.             if (strlen(op2S))
  694.                {strcpy(opS,",");
  695.                 strcat(opS,op2S);
  696.                 strcat(op1S,opS);
  697.                 }
  698.         i+=2;
  699.         my_printf("  %4lx:  %-7s  %-24s",iold,instS,op1S);
  700.         genasc();
  701.         genobj();
  702.         my_printf("%-16s  %s\n",objS,ascS);
  703.         }
  704.     /* Now free the storage we loaded the code into */
  705.     free(p);
  706.     }
  707.  
  708. #ifdef NEED_MAIN
  709. main(argc, argv)
  710. int argc;
  711. char **argv;
  712. {  register long  maxi=0, line=0;
  713.    register char ch;
  714.    FILE *fromfile, *tofile;
  715.    unsigned int hinib;
  716.    char *fromname, *toname, outname[40];
  717.  
  718.    if (argc < 2 || argc > 3)
  719.      {
  720.        printf("Useage: disass fromfile <tofile>\n");
  721.        printf("If tofile omitted, output will be directed to monitor.\n");
  722.        printf("If tofile specified, .asm will be appended to tofile.\n");
  723.        printf("\nWarning!! Output file may be about 13 times as big as \n");
  724.        printf("input file. Make sure you have enough memory.\n");
  725.        printf("Maximum input file size set for 20,000 bytes.\n");
  726.        printf("For maximum speed, send output to ram:.\n");
  727.        exit(4);
  728.      }
  729.   fromfile = fopen(argv[1], "r");
  730.   if (fromfile == NULL )
  731.      {
  732.        printf("Can't open: %s\n", argv[1]);
  733.        exit(4);
  734.      }
  735.   if (argc==3)
  736.      { sprintf(outname,"%s.asm",argv[2]);
  737.        tofile = fopen(outname, "w");
  738.        if (tofile == NULL )
  739.          { printf("Can't open: %s\n", argv[2]);
  740.            exit(4); }
  741.        printf("%s opened\n",argv[2]);
  742.        fprintf(tofile,"PC\tINST\tOPERANDS\t\tASCII\t HEX\n");
  743.      }
  744.   printf("Reading %s\n",argv[1]);
  745.   ch=fgetc(fromfile);
  746.   while (!feof(fromfile)  && maxi<maxfile)
  747.     {
  748.       p[maxi++]=ch;
  749.       ch=fgetc(fromfile);
  750.     }
  751.  
  752.   printf("number of bytes loaded %ld\n\n",maxi);
  753.   fclose( fromfile);  printf("Input file %s closed.\n\n",argv[1]);
  754.  
  755.     do {
  756.         printf("Enter a starting PC (program counter) between 0 and %ld ",maxi-2);
  757.         fflush(stdout);
  758.         scanf("%ld",&i);
  759.         }
  760.  
  761.   disass();
  762.  
  763.   if (argc==3)
  764.     { fclose( tofile );
  765.       printf("\nFinished.  Output file %s closed.\n",outname); }
  766.   exit(0);
  767. }
  768. #endif
  769.